package top.zimang.loveforwords.Service;

import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.Environment;
import android.os.IBinder;
import android.util.Log;

import androidx.localbroadcastmanager.content.LocalBroadcastManager;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.RandomAccessFile;
import java.util.Locale;

import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import top.zimang.loveforwords.Listener.DownloadListener;
import top.zimang.loveforwords.Task.Download;

public class Mp3Service extends Service {

    private Download download;
    private DownloadListener listener=new DownloadListener() {
        @Override
        public void onSuccess() {
            download =null;
            //下载成功时将前台服务关闭，并创建一个下载成功通知
            stopForeground(true);
            Intent intent=new Intent();
            intent.setAction("mp3Ready");
            LocalBroadcastManager.getInstance(Mp3Service.this).sendBroadcast(intent);
        }

        @Override
        public void onFailed() {
            download =null;
            //下载失败时将前台服务关闭，并创建一个下载失败通知
            stopForeground(true);
        }
    };
    private IBinder binder=  new MBinder();
    public class MBinder extends Binder {
        public Mp3Service getService(){
            return Mp3Service.this;
        }
    }
    @Override
    public IBinder onBind(Intent intent) {
        return binder;
    }

    public void download(String s){
        if (download==null){
            download=new Download(listener);
            download.execute(s);
        }
    }


}